package system.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import system.httpModel.Role;
import system.httpModel.User;
import system.service.RoleService;

import common.controller.BaseController;
import common.httpModel.EasyuiTreeNode;
import common.httpModel.Json;
import common.httpModel.SessionInfo;
import common.util.ResourceUtil;

/**
 * 角色控制器
 * @author jie
 *
 */
@Controller
@RequestMapping("/roleController")
public class RoleController extends BaseController {
	private RoleService roleService;

	public RoleService getRoleService() {
		return roleService;
	}
	
	@Autowired
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	/**
	 * 跳转到角色管理页面
	 * @return
	 */
	@RequestMapping(params="role")
	public String role(){
		return "/admin/role";
	}
	
	/**
	 * 角色treegrid
	 * @return
	 */
	@RequestMapping(params="treegrid")
	@ResponseBody
	public List<Role> treegrid(String id){
		
		return roleService.treegrid(id);
	}
	

	/**
	 * 角色树
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(String id) {
		return roleService.tree(id);
	}
	
	/**
	 * 添加角色
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping(params = "add")
	@ResponseBody
	public Json add(Role role) {
		Json j = new Json();
		Role r = roleService.add(role);
		j.setSuccess(true);
		j.setMsg("添加成功!");
		return j;
	}
	
	/**
	 * 编辑角色
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping(params = "edit")
	@ResponseBody
	public Json edit(HttpSession session,Role role) {
		Json j = new Json();
		SessionInfo sessionInfo=(SessionInfo) session.getAttribute(ResourceUtil.getSessionInfoName());
		User user=sessionInfo.getUser();
		Role r = roleService.edit(role,user);
		if(r!=null){
			j.setSuccess(true);
			j.setMsg("编辑成功!");	
		}else{
			j.setSuccess(false);
			j.setMsg("不能改给自己使用的角色权限！!");
		}
		
		return j;
	}
	
	/**
	 * 编辑角色
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public Json del(Role role) {
		Json j = new Json();
	    roleService.del(role);
		j.setSuccess(true);
		j.setMsg("编辑成功!");
		return j;
	}
}
